package net.towee.server.authentication;

import javax.servlet.http.HttpSession;

import net.towee.shared.exceptions.PersistenceException;

/**
 * Interface define some logic for authenticate user by
 * email and password.
 * 
 * If given from user email not exist in db, implementation
 * send an email message with confirm link. current session
 * set us authenticated, account data created but given email
 * not attached to account until user don't go to he confirm link.
 * It means that until user go to confirm link he cant authenticate
 * twice.
 *
 * @author c58
 */
public interface EmailAuth {
	public static final String EMAIL_AUTH_URL = "/auth/email";
	public static final String EMAIL_VALIDATION_ERROR = "invalid_email";
	public static final String PASSW_VALIDATION_ERROR = "invalid_password";
	public static final String ACC_NOT_EXISTS_ERROR = "account_not_exists";
	
	/**
	 * This method do some scenario
	 * 
	 * if exists "email" parameter and NOT exists "password" it create confirm entry,
	 * 		create account data and authenticate current session once.
	 * if exists "email" parameter and exists "password" it try authenticate user
	 *      On success it response redirect to home url.
	 */
	void processEmailAuthentication(String email, String password, HttpSession session, Appendable out);
	
	/**
	 * if exists "confirm" parameter it try confirm user email and attach it to
	 * 		account data.
	 */
	boolean processEmailConfirming(String confirm, HttpSession session, Appendable out);
	
	/**
	 * Try to confirm email by given unique string. If success confirm
	 * it remove confirm entry from db and attach email to account
	 * @param confirm
	 * @return true if unique success
	 */
	ConfirmEntry createConfirmEmailEntry(String confirm);
	
	/**
	 * Try to authenticate user by given email and password
	 * @param email
	 * @param password
	 * @return true if authentiation success
	 * @throws PersistenceException 
	 */
	String authenticateUser(String email, String password) throws PersistenceException;
	
	/**
	 * Create account data with given password
	 * @param password
	 * @return id of new account
	 */
	String createAccount(String password);
	
	/**
	 * Send an email message with confirm link and password
	 * @param email
	 * @param password
	 * @param confirm
	 * @return TODO
	 */
	boolean sendConfirmLink(String email, String password, String confirm);
}
